﻿using System;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace Model
{
	public class DrawRiserBridge : HYCmdEntry
	{
		public DrawRiserBridge(ExternalCommandData commandData) : base(commandData)
		{
		}

		public void ShowMainDlg(CableTray cableTrayOld)
		{
			try
			{
				if (this.drawBridge == null)
				{
					if (cableTrayOld != null)
					{
						this.drawBridge = new DlgDrawRiserBridge(this.m_doc, cableTrayOld);
					}
					else
					{
						this.drawBridge = new DlgDrawRiserBridge(this.m_doc);
					}
				}
				DialogResult dialogResult = this.drawBridge.ShowDialog();
				if (dialogResult != DialogResult.OK)
				{
					if (dialogResult != DialogResult.Cancel)
					{
						goto IL_78;
					}
				}
				else
				{
					if (this.drawBridge.SelectionMethod == 0)
					{
						this.SelectSlotDirection();
						goto IL_78;
					}
					if (this.drawBridge.SelectionMethod != 1)
					{
						goto IL_78;
					}
					this.DrawBridge(this.drawBridge, cableTrayOld);
				}
				return;
				IL_78:
				this.ShowMainDlg(cableTrayOld);
			}
			catch (Exception)
			{
			}
		}

		private void SelectSlotDirection()
		{
			try
			{
				XYZ xyz = this.m_sel.PickPoint("请选择桥架线槽方向第一点");
				XYZ xyz2 = (this.m_sel.PickPoint("请选择桥架线槽方向第二点,用此两点构成的方向与X轴的角度") - xyz).Normalize();
				double num = XYZ.BasisX.AngleOnPlaneTo(xyz2, XYZ.BasisZ);
				this.drawBridge.hytxtAngle.Text = (num / 0.017453292519943295).ToString("0");
			}
			catch (Exception)
			{
			}
		}

		public void Excute()
		{
			try
			{
				Reference reference = this.m_sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new RevitFilter.SelectionCableTrayFilter(), "请选择参照桥架");
				CableTray cableTrayOld = this.m_doc.GetElement(reference) as CableTray;
				this.ShowMainDlg(cableTrayOld);
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.ShowMainDlg(null);
			}
		}

		private void DrawBridge(DlgDrawRiserBridge drawBridge, CableTray cableTrayOld)
		{
			CableTrayType cableTrayType = drawBridge.cmbCableTrayType.SelectedItem as CableTrayType;
			string text = drawBridge.cmbSystemType.Text;
			double width = AssistFunc.mmToFeet(Convert.ToDouble(drawBridge.cmbWidth.Text));
			double height = AssistFunc.mmToFeet(Convert.ToDouble(drawBridge.cmbHeight.Text));
			double num = AssistFunc.mmToFeet(drawBridge.Bottom);
			double num2 = AssistFunc.mmToFeet(drawBridge.Top);
			for (;;)
			{
				Transaction transaction = new Transaction(this.m_doc, "DrawingCableTray");
				try
				{
					transaction.Start();
					double elevation = this.m_doc.ActiveView.GenLevel.Elevation;
					if (cableTrayOld != null)
					{
						ElementId elementId = cableTrayOld.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId();
						elevation = (this.m_doc.GetElementById(elementId) as Level).Elevation;
					}
					XYZ xyz = this.m_sel.PickPoint("请选择绘制桥架定位点");
					XYZ xyz2 = new XYZ(xyz.X, xyz.Y, num * 1000.0 + elevation);
					XYZ xyz3 = new XYZ(xyz.X, xyz.Y, num2 * 1000.0 + elevation);
					CableTray cableTray = CableTray.Create(this.m_doc, cableTrayType.Id, xyz2, xyz3, this.m_doc.ActiveView.GenLevel.Id);
					this.SetTrayParam(text, width, height, cableTray, cableTrayOld);
					HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTray);
					XYZ xyz4 = -hycableTray.CustomConnectorStart.Connector.CoordinateSystem.BasisY;
					double num3;
					if (drawBridge.rdoRight.Checked)
					{
						num3 = xyz4.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
					}
					else if (drawBridge.rdoBehind.Checked)
					{
						num3 = xyz4.AngleOnPlaneTo(XYZ.BasisY, XYZ.BasisZ);
					}
					else if (drawBridge.rdoLeft.Checked)
					{
						num3 = xyz4.AngleOnPlaneTo(-XYZ.BasisX, XYZ.BasisZ);
					}
					else if (drawBridge.rdoFront.Checked)
					{
						num3 = xyz4.AngleOnPlaneTo(-XYZ.BasisY, XYZ.BasisZ);
					}
					else
					{
						double num4 = xyz4.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
						num3 = Convert.ToDouble(drawBridge.hytxtAngle.Text.Trim());
						num3 = num3 * Math.PI / 180.0 + num4;
					}
					this.RotateTray(hycableTray, num3);
					this.m_doc.Regenerate();
					transaction.Commit();
					continue;
				}
				catch
				{
                    if (transaction != null && (int)transaction.GetStatus() != 3)
					{
						transaction.RollBack();
					}
				}
				break;
			}
		}

		private void RotateTray(HYCableTray<CableTray> hyCableTray, double angle)
		{
			Line line = Line.CreateUnbound(hyCableTray.StartXYZ, XYZ.BasisZ);
			hyCableTray.CableTray.Location.Rotate(line, angle);
		}

		private void SetTrayParam(string sysType, double width, double height, CableTray cableTrayNew, CableTray cableTrayOld)
		{
			cableTrayNew.get_Parameter(BuiltInParameter.RBS_CTC_SERVICE_TYPE).Set(sysType);
			cableTrayNew.get_Parameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width);
			cableTrayNew.get_Parameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height);
			if (cableTrayOld != null)
			{
				cableTrayNew.get_Parameter(BuiltInParameter.RBS_START_LEVEL_PARAM).Set(cableTrayOld.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId());
			}
		}

		public static bool isFirst = true;

		private DlgDrawRiserBridge drawBridge;
	}
}
